$(function () {
    /********返回******/
    var $back = $(".back");
    $back.on('touchstart', function () {
        var hasHistory = history.length;
        (history) ? (history.back()) : (window.location.href = 'index.html');
    })

})

var dialog = YDUI.dialog;
var bingUtil = {
    required: function (ele, str, reg) {
        if (!ele.val().trim()) {
            //ele.focus();
            YDUI.dialog.toast(str + '不能为空!', 'none', 1000);
            return false;
        }
        else if (reg) {
            //console.log(reg.test(ele.val()));
            if (reg.test(ele.val())) {
                return true;
            } else {
                YDUI.dialog.toast('请输入正确的' + str, 'none', 1000);
                return false;
            }
        } else {

            return true;
        }
    },
    myAjax: function (opt) {
        $.ajax({
            url: opt.url,
            type: opt.type || 'get',
            data: opt.data || '',
            success: function (res) {
                opt.callback && opt.callback(res);
            },
            error: function () {
                YDUI.dialog.alert("操作失败，请检查网络连接后重试！")
                YDUI.dialog.loading.close();
            }
        })
    }
};

/**流加载 */
var page =2,flagWaterFall = true;
function waterFall(opt) {
    page = opt.page;
    flagWaterFall=opt.flagWaterFall;
    var winHeight=$(window).height();
    console.log("调用",opt);
    $(window).on('scroll', function () {
        var oscrollTop = $(window).scrollTop(),
            docHeight = $(document).height();
        if ((oscrollTop + winHeight >= docHeight) && flagWaterFall) {
            console.log("----scroll waterFall----");
            getLists({
                data: opt.data,
                page:page,
                url: opt.url,
                cb: opt.cb || '',
                pageSize:opt.pageSize
            })
        }

    });
}
function getLists(req) {
    //console.log("请求：",req);
    var dataPage={
        page:page
    }
    var datas=Object.assign({},req.data,dataPage);
    //console.log(datas);
    if (flagWaterFall) {
        flagWaterFall=false;
        dialog.loading.open();
        $.ajax({
            url: req.url,
            type: 'get',
            data: datas,
            success: function (res) {
                dialog.loading.close();
                //console.log(res.data,res.data.length);
                var dataLen = res.data.length, html = '';
                page++;
                req.cb && req.cb(res);
                //console.log(dataLen+"-----------"+req.pageSize);
                if (dataLen < req.pageSize) {
                    dialog.toast('已加载全部数据', 'none', 900);
                    flagWaterFall = false;
                } else {
                    flagWaterFall = true;
                }
            },
            error: function () {
                dialog.loading.close();
                flagWaterFall = true;
            }
        })
    }
}